class Cow {
  constructor(age) {
    this.age = age
    this.alive = true
  }

  addAge() {
    this.age++
    if (this.age > 15) this.alive = false
  }

  productionCow() {
    // 如果小于5 或者已经死亡则返回null  否则就返回一个新出生的牛犊
    if (this.age < 5 || !this.isAlive()) {
      return null
    }
    return new Cow(0)
  }

  isAlive() {
    return this.alive
  }

}

class Form {
  constructor() {
    // 牧场 初始时有一个5岁的可以生育的母牛
    this.cowLists = [new Cow(5)]
  }
  getCowLists() {
    return this.cowLists
  }
}

class Main {
  constructor (){
    this.form = new Form()
    this.grow()
  }
  grow () {
    const cowLists = this.form.getCowLists()
    for (let i = 0; i < 20; i++) {
      let dead_num = 0
      let newCowArr = []
      cowLists.forEach(cow=>{
        cow.addAge()
        const child = cow.productionCow()
        if (child !=null) {
          newCowArr.push(child)
        }
        if (!cow.isAlive()) {
          dead_num++
        }
      })
      cowLists.push(...newCowArr)
      console.log(`第${i+1}年， 出生了${newCowArr.length}头,  死亡了${dead_num}头`)
    }
    for (let i = cowLists.length-1; i >=0; i--) {
      const cow = cowLists[i];
      if (!cow.isAlive()) {
        cowLists.splice(i,1)
      }
    }
    console.log(`总共还剩余${cowLists.length}头`)
  }
}

const main = new Main()